import config from '../../../src/config'

function getData(reqData){
  $.ajax({
       type: "POST",
       url: config.serverURI+'/demo/app/login',
       data: reqData,
       "headers": {
         "content-type": "application/json"
       },
       dataType: "json",
       success: function(res){
         // console.log( res.aggregations[2].buckets );
         var data = [];

         res.aggregations[2].buckets.forEach(function (item, key) {
           var subdata = [];
           var date = new Date();
           date.setTime(item.key);
           subdata.push(date.format('yyyy-MM-dd'));
           subdata.push(item.doc_count);
           data.push(subdata);
         });

         var option = {
               title: {
                   text: '用户登陆App次数/日'
               },
               tooltip: {
                   trigger: 'axis'
               },
               xAxis: {
                   data: data.map(function (item) {
                       return item[0];
                   })
               },
               yAxis: {
                   splitLine: {
                       show: false
                   }
               },
               toolbox: {
                   left: 'center',
                   feature: {
                       dataZoom: {
                           yAxisIndex: 'none'
                       },
                       restore: {},
                       saveAsImage: {}
                   }
               },
               dataZoom: [{
                   startValue: '2017-05-16'
               }, {
                   type: 'inside'
               }],
               visualMap: {
                   top: 10,
                   right: 10,
                   pieces: [{
                       gt: 0,
                       lte: 50,
                       color: '#096'
                   }, {
                       gt: 50,
                       lte: 100,
                       color: '#ffde33'
                   }, {
                       gt: 100,
                       lte: 150,
                       color: '#ff9933'
                   }, {
                       gt: 150,
                       lte: 200,
                       color: '#cc0033'
                   }, {
                       gt: 200,
                       lte: 300,
                       color: '#660099'
                   }, {
                       gt: 300,
                       color: '#7e0023'
                   }],
                   outOfRange: {
                       color: '#999'
                   }
               },
               series: {
                   name: '用户日登陆数据',
                   type: 'bar',
                   data: data.map(function (item) {
                       return item[1];
                   }),
                   markLine: {
                       silent: true,
                       data: [{
                           yAxis: 50
                       }, {
                           yAxis: 100
                       }, {
                           yAxis: 150
                       }, {
                           yAxis: 200
                       }, {
                           yAxis: 300
                       }]
                   }
               }
           }
         var chart2 = echarts.init(document.getElementById('loginBar'));
         chart2.setOption(option);
      }
   });
}

export { //很关键
  getData
}
